from tensorboard.backend.event_processing import event_accumulator
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from matplotlib.ticker import LinearLocator, FormatStrFormatter, MultipleLocator

#加载日志数据
ea=event_accumulator.EventAccumulator(r'F:\Unity_Project\FlightAndDodgeProject\Assets\ML-Agents\Training\results\SparseReward\FlyAndDodge\events.out.tfevents.1619011438.LAPTOP-THARA60B.12240.0') 
ea.Reload()
print(ea.scalars.Keys())

reward_data = ea.scalars.Items('Environment/Cumulative Reward')
episode_length_data = ea.scalars.Items('Environment/Episode Length')
reward_step = [reward.step for reward in reward_data]
reward_value = [reward.value for reward in reward_data]
episode_length_step = [episode_length.step for episode_length in episode_length_data]
episode_length_value = [episode_length.value for episode_length in episode_length_data]


# 画图
label_font = FontProperties(fname=r"C:\windows\fonts\simsun.ttc", size=14)
tick_size = 10

plt.figure(figsize=(10,5),dpi=200)
ax = plt.subplot(1,2,1)
plt.plot(reward_step, reward_value)
plt.xlabel('与环境交互次数',fontproperties=label_font)
plt.ylabel('累计奖励值',fontproperties=label_font)
plt.xticks(fontsize=tick_size)
plt.yticks(fontsize=tick_size)
plt.xlim(left=0)
plt.minorticks_on()
ax.ticklabel_format(axis='x', style='scientific', scilimits=(0,0))
plt.grid()


ax = plt.subplot(1,2,2)
plt.plot(episode_length_step, episode_length_value)
plt.xlabel('与环境交互次数',fontproperties=label_font)
plt.ylabel('幕长度/步',fontproperties=label_font)
plt.xticks(fontsize=tick_size)
plt.yticks(fontsize=tick_size)
plt.xlim(left=0)
plt.ylim(bottom=0)
plt.minorticks_on()
ax.ticklabel_format(axis='x', style='scientific', scilimits=(0,0))

plt.grid()
plt.show()
# plt.savefig('VisualHardRun',dpi=300)



